package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.BusinessAbodeBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.BusinessAbodeMapper;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.ValidationKey;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 添加营业所
 */
public class BusinessAbodeAdd implements ServerInterface {

	private static Logger logger = LogManager.getLogger(BusinessAbodeAdd.class);

	@Override
	@ValidationKey(clazz = BusinessAbodeBean.class,method="insert")
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			BusinessAbodeMapper mapper = factory.getMapper(BusinessAbodeMapper.class);
			BusinessAbodeBean bean = JsonUtils.objectToPojo(req.getData(), BusinessAbodeBean.class);
			// 校验字段重复
			if (mapper.check("abode_name", bean.getAbodeName()) > 0) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "营业所名称：" + bean.getAbodeName() + "的信息已存在。", void.class);
			}
			if (mapper.check("abode_no", bean.getAbodeNo()) > 0) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "营业所编号：" + bean.getAbodeNo() + "的信息已存在。", void.class);
			}
			bean.setCreateInfo(userInfo);
			bean.setGroupCode(userInfo.getCurWaterCode());
			mapper.insertBusinessAbode(bean);
		} catch (Exception e) {
			logger.error("营业所添加失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "营业所添加失败", void.class);
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
	}
}
